//
//  UserDBTools.m
//  NC-Trendy
//
//  Created by LeungChaos on 16/2/26.
//  Copyright © 2016年 liang. All rights reserved.
//

#import "UserDBTools.h"

@implementation UserDBTools

+ (NSManagedObjectContext *)objContext {
    return [AppDelegate shareAppDelegate].managedObjectContext;
}

+ (NSFetchRequest *)fetchRequest {
    return [NSFetchRequest fetchRequestWithEntityName:@"CoreUser"];
}

+ (NSEntityDescription *)entityDesc {
    return [NSEntityDescription entityForName:@"CoreUser" inManagedObjectContext:[self objContext]];
}

+ (CoreUser *)coreUserWithUser_id:(NSString *)u_id errorInfo:(void(^)(NSError *error))errorBlock {
    NSError *error;
    NSFetchRequest *fetchRequset = [self fetchRequest];
    fetchRequset.predicate = [NSPredicate predicateWithFormat:@"user_id = %@",u_id];
    NSArray *array = [[self objContext] executeFetchRequest:fetchRequset error:&error];
    if (array.count == 1) {
        CoreUser *cUser = [array lastObject];
        return cUser;
    } else {
        return nil;
    }
}

+ (void)coreUserInsertWithUserModel:(UserModel *)user errorInfo:(void(^)(NSError *error))errorBlock {
    NSError *error;
    NSFetchRequest *fetchRequset = [self fetchRequest];
    fetchRequset.predicate = [NSPredicate predicateWithFormat:@"user_id = %@",user.user_id];
    NSArray *array = [[self objContext] executeFetchRequest:fetchRequset error:&error];
    if (array.count > 0) {
        return;
    } else {
        CoreUser *cUser = [[CoreUser alloc]initWithEntity:[self entityDesc] insertIntoManagedObjectContext:[self objContext]];
        cUser.user_icon = user.user_icon;
        cUser.user_id = user.user_id;
        cUser.user_name = user.user_name;
        cUser.user_email = user.user_email;
        cUser.user_pwd = user.user_pwd;
        cUser.user_sex = user.user_sex;
        cUser.user_sicon = user.user_sicon;
        cUser.user_tel = user.user_tel;
        [[self objContext]insertObject:cUser];
        [[self objContext]save:&error];
    }
    if (errorBlock) {
        errorBlock(error);
    }
}

+ (void)deleteAllUserInfoWithUser_id:(NSString *)user_id errorInfo:(void(^)(NSError *error))errorBlock {
   __block NSError *error;
    NSFetchRequest *fetchRequset = [self fetchRequest];
    fetchRequset.predicate = [NSPredicate predicateWithFormat:@"user_id = %@",user_id];
    NSArray *array = [[self objContext] executeFetchRequest:fetchRequset error:&error];
    
    NSArray *allUserArray = [[self objContext]executeFetchRequest:[self fetchRequest] error:&error];
    if (allUserArray.count > 0) {
        [allUserArray enumerateObjectsUsingBlock:^(CoreUser * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [[self objContext]deleteObject:obj];
            [[self objContext]save:&error];
        }];
    }
    if (array.count > 0) {
        CoreUser *cUser = [array lastObject];
        [[self objContext]insertObject:cUser];
        [[self objContext]save:&error];
    }
    if (errorBlock) {
        errorBlock(error);
    }
}

+ (UserModel *)userWithCoreUser:(CoreUser *)cUser {
    UserModel *user = [[UserModel alloc]init];
    user.user_icon = cUser.user_icon;
    user.user_id = cUser.user_id;
    user.user_name = cUser.user_name;
    user.user_email = cUser.user_email;
    user.user_pwd = cUser.user_pwd;
    user.user_sex = cUser.user_sex;
    user.user_sicon = cUser.user_sicon;
    user.user_tel = cUser.user_tel;
    return user;
}

@end



